#!/bin/sh
# based on the Slackware 12.0 SlackBuild
# Packager Rob Cotrone  ( WeeboTech at mydomain dot com)
# http://lime-technology.com
# Required: 

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
NAME=locate+webGui
PKG=$TMP/package-${NAME}
VERSION=1.0.1
ARCH=${ARCH:-unRAID}
BUILD=WeeboTech
SOURCE=http://unraid-webgui.googlecode.com/files/${NAME}-${VERSION}.tar.gz

if [ "$1" = "--makedist" ]
   then echo "Building SOURCE Distribution: ${NAME}-${VERSION}.tar.gz"	
	echo
 	rm -vf "$NAME-$VERSION-$ARCH-$BUILD.tgz" "${NAME}-${VERSION}.tar.gz"
        tar -C .. -cvzf ${NAME}-${VERSION}.tar.gz ${NAME}-${VERSION}
	echo
	ls -l ${NAME}-${VERSION}.tar.gz
	echo
	exit
fi

if [ ! -e $NAME-$VERSION.tar.gz ]
   then echo wget -c $SOURCE
fi

if [ ! -d $TMP ]
   then mkdir -p $TMP
fi

if [ ! -d $PKG ]
   then mkdir -p $PKG
   else rm -rf ${PKG}
        mkdir ${PKG}
fi

cd $TMP

if [ -f $CWD/$NAME-$VERSION.tar.gz ]
   then echo "Using SOURCE Distribuition: $CWD/$NAME-$VERSION.tar.gz"
	tar xvzf $CWD/$NAME-$VERSION.tar.gz
   else echo "missing SOURCE Distribution: $CWD/$NAME-$VERSION.tar.gz"
	echo "Assuming files are in ${CWD}"
        sleep 1
fi

echo -e "\n\E[0;32mStart SlackBuild $NAME-$VERSION\E[0;0m\n"

if [ ! -d $NAME-$VERSION ] 
   then mkdir -p $NAME-$VERSION
fi

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

# chown -R root:root .

# Here is where the manual build install commands go.

WEBGUIDIR="usr/local/webGui"
install -d ${PKG}/${WEBGUIDIR}
while read FILE
do  [ -z "${FILE%\#*}" ] && continue
    F=${FILE##*/}           # basename of file
    D=${FILE%%$F}           # dirname of file
    B=${F%.*}               # strip off after last . character
    E=${F#${B}\.}           # get extension after last .
    # echo "D=${D}, F=${F}, B=${B}, E=${E}"
    install -p -m550 ${FILE} ${PKG}/${WEBGUIDIR}
    if [ "${E}" = "page" ]
       then sed -i "s/Version=.*/Version=\"${VERSION}\"/g" ${PKG}/${WEBGUIDIR}/${F}
    fi
done <<-EOF
# List your files here. You can comment them out if needed.
${CWD}/locate.php
${CWD}/locate.png
${CWD}/locate.page
${CWD}/updatedb.page
${CWD}/updatedb.png
EOF

# The rest should be standard on all installations

# mkdir -p ${PKG}/usr/man
mkdir -p ${PKG}/install
mkdir -p ${PKG}/usr/doc/${NAME}-${VERSION}
sed "s/#.##/${VERSION}/g" < ${CWD}/slack-desc > ${PKG}/install/slack-desc
sed "s/#.##/${VERSION}/g" < ${CWD}/slack-desc > ${PKG}/usr/doc/${NAME}-${VERSION}/slack-desc
cat ${CWD}/${NAME}.SlackBuild > ${PKG}/usr/doc/${NAME}-${VERSION}/${NAME}.SlackBuild

if [ -f ${CWD}/doinst.sh ]
   then cat $CWD/doinst.sh  > $PKG/install/doinst.sh
fi

if [ -d ${PKG}/usr/man ]
   then find "${PKG}/usr/man" -type f -exec gzip {} \;
fi

cd ${PKG}
# requiredbuilder -y -v -s ${CWD} ${PKG}
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]
   then rm -rf $TMP
	shift
fi

if [ -f ${CWD}/${NAME}-${VERSION}.tar.gz ]
   then ls -l ${CWD}/${NAME}-${VERSION}.tar.gz
fi

if [ -f ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz ]
   then ls -l ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz
fi

if [ "$1" = "--install" ]
   then echo;installpkg ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz
fi

if [ "$1" = "--deploy" ]
   then echo;cp -v ${CWD}/$NAME-$VERSION-$ARCH-$BUILD.tgz /boot/extra
        shift
fi
